package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.union.huaben.UnionHuaBenPatrons;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_UNION_HUA_BEN_PATRONS_DISPATCH, desc = "门客派遣")
public class CrossUnionHuaBenDispatchCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionHuaBenProto.UnionHuaBenDispatchCrossReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenDispatchCrossReqMsg.parseFrom(packet.getBytes());

        int idx = reqMsg.getIdx();
        List<UnionHuaBenPatrons> patronsList = new ArrayList<>();
        for (UnionHuaBenProto.UnionHuaBenPatronsInfoTemp patronsInfoTemp : reqMsg.getPatronsList()) {
            UnionHuaBenPatrons patrons = new UnionHuaBenPatrons();
            patrons.setPatronsId(patronsInfoTemp.getPatronsId());
            patrons.setAbility(patronsInfoTemp.getAbility());
            patrons.setOccupat(patronsInfoTemp.getOccupation());
            patrons.setAddition(patronsInfoTemp.getAddition());
            patronsList.add(patrons);
        }

        //
        UnionHuaBenProto.UnionHuaBenDispatchCrossRespMsg.Builder builder = CrossUnionHuaBenMgr.dispatchFight(serverId, packet.getPlayerId(), idx, patronsList, reqMsg.getLevel());
        if(builder.getRet() != 0) {
            for (UnionHuaBenPatrons patrons : patronsList) {
                builder.addPatronsId(patrons.getPatronsId());
            }
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_CROSS_UNION_HUA_BEN_PATRONS_DISPATCH_FROM_CROSS,
                builder));

    }

    @Override
    public void sendData() {

    }
}
